<template>
  <div>
    <!-- 面包屑导航 -->
    <Breadcrumb :breadcrumbInfo="breadcrumbInfo"></Breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="15">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            clearable
            v-model="queryInfo.query"
            @clear="searchGoods"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchGoods"
              >搜索</el-button
            >
          </el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区 -->
      <el-table
        :data="GoodsListInfo.GoodsList.goods"
        stripe
        border
        highlight-current-row
      >
        <el-table-column align="center" type="index"></el-table-column>
        <el-table-column align="center" prop="goods_name" label="商品名称">
        </el-table-column>
        <el-table-column
          align="center"
          prop="goods_price"
          label="商品价格(元)"
          width="120"
        >
        </el-table-column>
        <el-table-column
          align="center"
          width="100"
          prop="goods_number"
          label="商品数量"
        >
        </el-table-column>
        <el-table-column
          align="center"
          width="100"
          prop="goods_weight"
          label="商品重量"
        >
        </el-table-column>
        <el-table-column align="center" prop="add_time" label="创建时间">
          <!-- 时间格式化 -->
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作部分 -->
        <el-table-column label="操作" width="200px" align="center">
          <!-- 编辑 删除 按钮 -->
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="getEditGoods(scope.row)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[20, 50, 100, GoodsListInfo.total]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="GoodsListInfo.total"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="50%">
      <el-form :rules="editGoodsRules" label-width="80px" :model="editForm">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="upEditGoods()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Breadcrumb from "../../components/Breadcrumb.vue";
export default {
  name: "Goods",
  components: { Breadcrumb },
  data() {
    return {
      // 商品列表面包屑导航信息
      breadcrumbInfo: {
        titlePath: [
          { title: "首页", Path: "/Home" },
          { title: "商品管理", Path: "" },
          { title: "商品列表", Path: "" },
        ],
      },
      //  请求参数对象
      queryInfo: {
        query: "",
        //当前页数
        pagenum: 1,
        //当前页显示最大是数据
        pagesize: 20,
      },
      //商品列表信息
      GoodsListInfo: {
        //   商品列表数据
        GoodsList: {},
        //商品列表总数
        total: 0,
      },
      //要编辑的商品列表信息
      editForm: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
      },
      //要编辑的商品列表信息
      // 是否显示编辑商品对话框
      editDialogVisible: false,
      // 上传的图片临时路径（对象）
      pics: {},
      // 商品的参数（数组）
      attrs: [],

      editGoodsRules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //监听页码改变时会触发
    handleCurrentChange(newPagenum) {
      this.queryInfo.pagenum = newPagenum;
      //   从服务器获取数据
      this.getGoodsList();
    },
    //监听页面可以显示大小改变时会触发
    handleSizeChange(newPagesize) {
      this.queryInfo.pagesize = newPagesize;
      //   从服务器获取数据
      this.getGoodsList();
    },

    //   从服务器获取商品列表信息
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }
      this.GoodsListInfo.GoodsList = res.data;
      this.GoodsListInfo.total = res.data.total;
    },
    //搜索商品
    searchGoods() {
      this.getGoodsList();
    },

    //添加商品
    goAddpage() {
      this.$router.push("/goods/add");
    },

    // 获取要编辑的商品
    async getEditGoods(goods) {
      const { data: res } = await this.$http.get(`goods/${goods.goods_id}`);
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.status);
      }
      this.editForm = res.data;
      this.editDialogVisible = true;
    },

    // 编辑商品
    async upEditGoods() {
      const { data: res } = await this.$http.put(
        "goods/" + this.editForm.goods_id,
        this.editForm
      );
      if (res.meta.status !== 200) {
        return this.$message.error("更新商品信息失败!");
      }
      this.$message.success(res.meta.msg);
      this.getGoodsList();
      this.editDialogVisible = false;
    },
    // 删除商品
    deleteGoods(goodsId) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete("goods/" + goodsId);
          if (res.meta.status !== 200) {
            return this.$message.error("删除商品信息失败!");
          }
          this.getGoodsList();
          this.$message.success("删除商品信息成功!");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.el-pagination {
  margin-top: 15px;
}
</style>